package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.PurgatoryMapper;
import com.xgame.server.db.model.Purgatory;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class PurgatoryCsTool {

    @Resource
    private PurgatoryMapper purgatoryMapper;

    //插入玩家记录
    public Integer insertData(Game.PurgatoryCs data) throws Exception {
        Integer result = new Integer(0);

        Purgatory account = new Purgatory();
        account.setAccount(data.getAccount());
        account.setPlayerId(data.getPlayerID());

        String lst = "";
        List<Game.ShopOneCs> items = data.getShopOneLstList();
        int length = items.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout();
            } else {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout() + "^";
            }
        }
        account.setShopOneLst(lst);

        account.setOneRefresh(data.getOneRefresh());
        account.setLastDieTime(data.getLastDieTime());
        account.setSpawnInterval(data.getSpawnInterval());
        account.setCurrentMap(data.getCurrentMap());
        account.setTotalOrder(data.getTotalOrder());
        account.setCurrentStar(data.getCurrentStar());
        account.setLastRefreshNum(data.getLastRefreshNum());

        lst = "";
        int size = data.getCompareItemList().size();
        for (int i = 0; i < size; i++) {
            if (i == size - 1) {
                lst += data.getCompareItem(i).getId() + "*" + data.getCompareItem(i).getCount();
            } else {
                lst += data.getCompareItem(i).getId() + "*" + data.getCompareItem(i).getCount() + ";";
            }
        }
        account.setCompareItem(lst);

        lst = "";
        lst = data.getOneCS().getLastDieTime() + ";" + data.getOneCS().getSpawnInterval();
        account.setOneCs(lst);
        account.setFreeRefresh(data.getFreeRefresh());
        account.setBatRefresh(data.getBatRefresh());

        return purgatoryMapper.insert(account);
    }

    //更新玩家记录
    public Integer updateData(Game.PurgatoryCs data) throws Exception {
        EntityWrapper<Purgatory> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", data.getAccount());

        Purgatory account = new Purgatory();
        account.setAccount(data.getAccount());
        account.setPlayerId(data.getPlayerID());

        String lst = "";
        List<Game.ShopOneCs> items = data.getShopOneLstList();
        int length = items.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout();
            } else {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout() + "^";
            }
        }
        account.setShopOneLst(lst);

        account.setOneRefresh(data.getOneRefresh());
        account.setLastDieTime(data.getLastDieTime());
        account.setSpawnInterval(data.getSpawnInterval());
        account.setCurrentMap(data.getCurrentMap());
        account.setTotalOrder(data.getTotalOrder());
        account.setCurrentStar(data.getCurrentStar());
        account.setLastRefreshNum(data.getLastRefreshNum());

        lst = "";
        int size = data.getCompareItemList().size();
        for (int i = 0; i < size; i++) {
            if (i == size - 1) {
                lst += data.getCompareItem(i).getId() + "*" + data.getCompareItem(i).getCount();
            } else {
                lst += data.getCompareItem(i).getId() + "*" + data.getCompareItem(i).getCount() + ";";
            }
        }
        account.setCompareItem(lst);

        lst = "";
        lst = data.getOneCS().getLastDieTime() + ";" + data.getOneCS().getSpawnInterval();
        account.setOneCs(lst);
        account.setFreeRefresh(data.getFreeRefresh());
        account.setBatRefresh(data.getBatRefresh());
        return purgatoryMapper.update(account, userWrapper);
    }

    //查询记录
    public List<Purgatory> selectPurgatory(String account) throws Exception {
        EntityWrapper<Purgatory> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return purgatoryMapper.selectList(userWrapper);
    }
}
